package com.tdk.wifidirectserver.socket.client

import android.util.Log
import java.net.Socket

class Client(socket: Socket? = null, ip: String? = null, port: Int? = null) :
    IClientAction {

    var ip: String? = null
    var port: Int? = null
    var socket: Socket? = null

    init {
        this.ip = ip
        this.port = port
        this.socket = socket
    }

    val TAG = "Client"
    private var mIClientListener: IClientListener? = null
    private var clientThread: ClientThread? = null


    fun setIClientListener(iClientListener: IClientListener?): Client {
        mIClientListener = iClientListener
        return this
    }

    fun connect() {
        Log.d(TAG, "connect:ip:[$ip]  port:[$port] ")
        if (ip.isNullOrEmpty() || port == null) {
            return
        }
        clientThread = ClientThread(this, mIClientListener)
        clientThread!!.start()
    }

    override fun disconnect() {
        clientThread?.disconnect()
    }


    fun sendData(data: String?) {
        Log.d(TAG, "sendData:data:[$data] ")
        data?.let {
            clientThread?.sendData(data)
        }

    }

    override fun sendData(data: ByteArray?) {
        data?.let {
            clientThread?.sendData(data)
        }
    }


}